
package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.PlatOrganizationCategory;
import com.hoshiicloud.user.param.organization.PlatOrganizationCategoryAddParam;
import com.hoshiicloud.user.param.organization.PlatOrganizationCategoryQueryParam;
import com.hoshiicloud.user.param.organization.PlatOrganizationCategoryUpdateParam;
import com.hoshiicloud.user.service.organization.PlatOrganizationCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 * 机构经营类目plat_organization_category 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-08-21
 */

@Slf4j
@RestController
@RequestMapping("/organization/plat-organization-category")
@Api(value = "SAAS租户供应商经营类目", tags = {"SAAS租户供应商经营类目"})
@ApiSort(25)
public class PlatOrganizationCategoryController {
    @Autowired
    private PlatOrganizationCategoryService platOrganizationCategoryService;


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询机构经营类目plat_organization_category
     */
    @ApiOperation(value = "分页查询机构经营类目plat_organization_category")
    @PostMapping("/getPage")
    public Response page(@RequestBody PlatOrganizationCategoryQueryParam param) {
        try {
            QueryWrapper<PlatOrganizationCategory> queryWrapper = new QueryWrapper<PlatOrganizationCategory>();
            queryWrapper.lambda()
                    .eq((param.getPlatformId() != null && param.getPlatformId() != 0), PlatOrganizationCategory::getPlatformId, param.getPlatformId())
                    .eq((param.getCategoryId1() != null && param.getCategoryId1() != 0), PlatOrganizationCategory::getCategoryId1, param.getCategoryId1())
                    .eq((param.getCategoryId2() != null && param.getCategoryId2() != 0), PlatOrganizationCategory::getCategoryId2, param.getCategoryId2())
                    .eq((param.getCategoryId3() != null && param.getCategoryId3() != 0), PlatOrganizationCategory::getCategoryId3, param.getCategoryId3())
                    .eq((param.getWeight() != null && param.getWeight() != 0), PlatOrganizationCategory::getWeight, param.getWeight())
                    .eq((param.getOrdered() != null && param.getOrdered() != 0), PlatOrganizationCategory::getOrdered, param.getOrdered())
                    .eq((param.getEnabled() != null && param.getEnabled() != 0), PlatOrganizationCategory::getEnabled, param.getEnabled())
                    .eq((param.getDeleted() != null && param.getDeleted() != 0), PlatOrganizationCategory::getDeleted, param.getDeleted());
            Page<PlatOrganizationCategory> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(platOrganizationCategoryService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询机构经营类目plat_organization_category异常", e);
            return Response.failed("分页查询机构经营类目plat_organization_category异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询机构经营类目plat_organization_category
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询机构经营类目plat_organization_category")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(platOrganizationCategoryService.getById(id));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增机构经营类目plat_organization_category")
    public Response save(@RequestBody @Valid PlatOrganizationCategoryAddParam param) {
        PlatOrganizationCategory data;
        try {
            data = BeanUtils.covertType(param, PlatOrganizationCategory.class);
        } catch (Exception e) {
            log.error("新增机构经营类目plat_organization_category Bean转换异常", e);
            return Response.failed("新增机构经营类目plat_organization_category Bean转换异常");
        }
        return Response.data(platOrganizationCategoryService.save(data));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改机构经营类目plat_organization_category
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改机构经营类目plat_organization_category")
    public Response update(@RequestBody @Valid PlatOrganizationCategoryUpdateParam param) {
        PlatOrganizationCategory data;
        try {
            data = BeanUtils.covertType(param, PlatOrganizationCategory.class);
        } catch (Exception e) {
            log.error("修改机构经营类目plat_organization_category Bean转换异常", e);
            return Response.failed("修改机构经营类目plat_organization_category Bean转换异常");
        }
        return Response.data(platOrganizationCategoryService.updateById(data));
    }

    @PostMapping("/insertCategory4PlatOrganization")
    @ApiOperation(value = "批量新增租户供应商经营类目plat_organization_category")
    public Response insertCategory4ComOrganization(@RequestBody List<PlatOrganizationCategory> platOrganizationCategories){
        return Response.data(platOrganizationCategoryService.insertCategory4PlatOrganization(platOrganizationCategories));
    }
}

